package com.cloudptt.api.product.func.call.single;

import com.cloudptt.api.pjsip.api.SipCallSession;
import com.cloudptt.api.pjsip.api.SipManager;
import com.cloudptt.api.product.func.IBroadcast;
import com.cloudptt.api.product.func.call.CallFunc;
import com.cloudptt.api.product.log.Log;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class SingleCallBroadcast extends BroadcastReceiver implements IBroadcast
{
	

	private Context ctx = null;
	private SingleCallTracker callTracker = null;

	public SingleCallBroadcast(Context ctx,SingleCallTracker callTracker)
	{
		this.ctx = ctx;
		this.callTracker = callTracker;

		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SINGLE_CALL_INCOMING));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SINGLE_CALL_INFO));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_MEDIA_CHANGED));
	}


	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.d(SingleCallTracker.THIS_FILE, "Action is " + action);

		if (action.equals(SipManager.ACTION_SINGLE_CALL_INCOMING)) 
		{
			SipCallSession callSession = intent.getParcelableExtra(SipManager.EXTRA_CALL_INFO);
			Log.d(SingleCallTracker.THIS_FILE, "ACTION_SIP_CALL_INCOMING callSession is " + callSession);
			callTracker.incomingCall(callSession);
			
		}
		else if (action.equals(SipManager.ACTION_SINGLE_CALL_INFO)) 
		{
			SipCallSession callSession = intent.getParcelableExtra(SipManager.EXTRA_CALL_INFO);
			Log.d(SingleCallTracker.THIS_FILE, "ACTION_SINGLE_CALL_INFO callSession is " + callSession);
			callTracker.recvPCallSession(callSession);
		}
		else if (action.equals(SipManager.ACTION_SIP_MEDIA_CHANGED)) {
			int mediaStatus = intent.getIntExtra(SipManager.EXTRA_MEDIA_STATUS,0);
			Log.d(SingleCallTracker.THIS_FILE, "ACTION_SINGLE_CALL_INFO mediaStatus is " + mediaStatus);
			String callKey = intent.getStringExtra(SipManager.EXTRA_CALL_KEY);
			callTracker.receivePCallMediaStatus(callKey,mediaStatus);
		}
	}

	public void clearResource()
	{
		Log.d(SingleCallTracker.THIS_FILE, "GroupCallBroadcast clearResource");
		this.ctx.unregisterReceiver( this );
		this.ctx = null;
		this.callTracker = null;
//		this.curCallSession = null;
	}
}
